package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ciko.code.bean.WorkMeetingListBean;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.http.Req.GetWorkMeetingListReq;
import com.ciko.code.utils.NetWorkUtils;
import com.ciko.code.views.pullrefresh.PullToRefreshBase;
import com.ciko.code.views.pullrefresh.PullToRefreshScrollView;
import com.multi_image_selector.view.MyListView;
import com.mywork.activity.WorkMeetingDetailActivity;
import com.mywork.adapter.MeetingsAdapter;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000185.R;
import com.tgx.tina.android.ipc.framework.IUIPage;

import java.util.ArrayList;
import java.util.List;

import base.tina.core.task.infc.ITaskResult;

/**
 * 会议签到界面
 */
public class WorkMeetingPage extends AbstractUIPage<MainActivity> implements HttpResponseListener {

    private MeetingsAdapter adapter1, adapter2;
    private long lastSerialCode = 0;                 //最后一次序列号
    private int PAGESIZE = 1000;                 //每页长度
    private List<WorkMeetingListBean> list1 = new ArrayList<>();
    private List<WorkMeetingListBean> list2 = new ArrayList<>();
    private Context context;
    private PullToRefreshScrollView sV;
    private RelativeLayout rlNoneTip;
    private boolean isRefresh = true;
    private List<WorkMeetingListBean> list = new ArrayList<>();
    private MyListView lv1, lv2;                             //lv1最近会议  lv2以往会议
    private LinearLayout llNew, llOld;
    private ScrollView scrollView;
    private Handler handler = new Handler();
    private static WorkMeetingPage instance;

    private ImageView zxing;

    private TextView tvTitle;
    private ImageView ivPersion;

    public static WorkMeetingPage getInstance(MainActivity t) {
        /*if (null == instance) */
        instance = new WorkMeetingPage(t);
        return instance;
    }

    public WorkMeetingPage(MainActivity t) {
        super(t);
    }

    @Override
    public <E extends Activity> View createView(E arg0, int arg1) {
        if (null != curMyView) return curMyView;
        curMyView = bActivity.getLayoutInflater().inflate(R.layout.class_schedule_page_layout, null);

        context = bActivity;
        initView();
        setLinstener();
        return curMyView;
    }

    private void setLinstener() {
        sV.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                refresh();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                loadMore();
            }
        });
        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent1 = new Intent(context, WorkMeetingDetailActivity.class);
                intent1.putExtra("meetingId", list1.get(i).getMeetingId());
                bActivity.startActivity(intent1);
            }
        });

        lv2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent2 = new Intent(context, WorkMeetingDetailActivity.class);
                intent2.putExtra("meetingId", list2.get(i).getMeetingId());
                bActivity.startActivity(intent2);
            }
        });
    }

    private void initView() {
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText("培训安排");
        ivPersion = (ImageView) curMyView.findViewById(R.id.common_header_persion);
        ivPersion.setVisibility(View.VISIBLE);//用户登录按钮设置成可见
        ivPersion.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                bActivity.goPersonalActivity();
            }
        });
        zxing = (ImageView) curMyView.findViewById(R.id.zxing);
        zxing.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                bActivity.goMipcaActivityCapture();
            }
        });
        sV = (PullToRefreshScrollView) curMyView.findViewById(R.id.sv);
        sV.setPullLoadEnabled(true);
        sV.setScrollLoadEnabled(true);
        lv1 = (MyListView) curMyView.findViewById(R.id.lv_latest_meetings);
        lv2 = (MyListView) curMyView.findViewById(R.id.lv_old_meetings);
        rlNoneTip = (RelativeLayout) curMyView.findViewById(R.id.rl_none_tip_logo);
        llNew = (LinearLayout) curMyView.findViewById(R.id.ll_new);
        llOld = (LinearLayout) curMyView.findViewById(R.id.ll_old);
        llOld.setVisibility(View.GONE);
        llNew.setVisibility(View.GONE);
        scrollView = (ScrollView) curMyView.findViewById(R.id.scrollView);

        adapter1 = new MeetingsAdapter(context, list1, 1);//1最新会议
        adapter2 = new MeetingsAdapter(context, list2, 2);//2以往会议
        lv1.setAdapter(adapter1);
        lv2.setAdapter(adapter2);

        HttpClient.getInstance(bActivity).post(new GetWorkMeetingListReq(lastSerialCode, PAGESIZE), this);
    }

    private void refresh() {
        isRefresh = true;
        lastSerialCode = 0;
        HttpClient.getInstance(bActivity).post(new GetWorkMeetingListReq(lastSerialCode, PAGESIZE), this);

    }

    private void loadMore() {
        isRefresh = false;
        HttpClient.getInstance(bActivity).post(new GetWorkMeetingListReq(lastSerialCode, PAGESIZE), this);
    }

    @Override
    public int enter(IUIPage<?> arg0) {

        refresh();
        return 0;
    }

    @Override
    public int leave(IUIPage<?> arg0) {
        return 0;
    }

    @Override
    public void notifyView(int arg0, Bundle arg1) {

    }

    @Override
    public void setStatus(int arg0) {

    }

    @Override
    public View updateView(int arg0, ITaskResult arg1) {
        return null;
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        super.onResponseSuccess(response, tag);
        if (isRefresh) {
            list.clear();
            list1.clear();
            list2.clear();
            list = (List<WorkMeetingListBean>) response.getReturnObject();
        } else {
            list.addAll((List<WorkMeetingListBean>) response.getReturnObject());
        }
        if (list.size() == 0) {
            rlNoneTip.setVisibility(View.VISIBLE);
            return;
        }

        sV.onPullDownRefreshComplete();
        sV.onPullUpRefreshComplete();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getType() == 0) {
                list2.add(list.get(i));
            } else {
                list1.add(list.get(i));
            }
        }

        if (list1.size() == 0) {
            llNew.setVisibility(View.GONE);
        } else {
            llNew.setVisibility(View.VISIBLE);
        }
        if (list2.size() == 0) {
            llOld.setVisibility(View.GONE);
        } else {
            llOld.setVisibility(View.VISIBLE);
        }
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
        handler.post(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0, 0);
            }
        });
        lastSerialCode = list.get(list.size() - 1).getSerialCode();
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        super.onResponseFail(tag, resultCode);
        if (NetWorkUtils.isConnected(context))
            com.ciko.code.utils.Util.showToast(context, "加载失败，请重试");
    }
}
